package cn.jekun.shop.common.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.validator.annotation.Security;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.service.gen.RsUserService;
import cn.jekun.shop.app.bean.Member;

@Service(RewardService.BEAN_NAME)
public class RewardService extends ShopBaseService{
	public static final String BEAN_NAME = "cn.jekun.shop.common.service.RewardService";
	@Override
	public String getNamespace() {
		return "SHOP_MY_REWARD";
	}
	@Resource(name = RsUserService.BEAN_NAME)
	RsUserService rsUserService;
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public JSONObject getMyRewards(
			@Security
			String currentPage,
			@Security
			String pageSize,
			@Security
			String userId) {
		Page page = new Page();
		JSONObject jsonObject = new JSONObject();
		if(StringUtils.isBlank(userId)){
			throw new BusinessException(Constants.ERRORS.VALIDATE_ERROR_CODE,
					"用户代码不能为空");
		}
		if(StringUtils.isNotBlank(pageSize)){
			page.setPageSize(Integer.parseInt(pageSize));
		}
		if(StringUtils.isNotBlank(currentPage)){
			page.setPageNumber(Integer.parseInt(currentPage));
		}
		Map tempMap = new HashMap();
		tempMap.put("shopId", Constants.COM_SHOP_ID);
		tempMap.put("userId", userId);
		tempMap.put("operationTypeId", Constants.MC_CREDITS_OPERATION_TYPE.REG_ADD);
		List<Member> list = this.list(tempMap, page);
		String sql = "select sum(c.credits) as count from (select b.credits from rs_user a" + " left join mc_credits_journal b on(a.user_id=b.ext) "  + " where a.invite_user_id='" + userId
				+ "' and b.shop_id='" + Constants.COM_SHOP_ID + "' and b.operation_type_id='" + Constants.MC_CREDITS_OPERATION_TYPE.REG_ADD + "') c";
		List<Map> listResult = this.listBySql(sql);
		String totalcredits = listResult.get(0).get("COUNT")==null?"0":listResult.get(0).get("COUNT").toString();
		jsonObject.put("totalcredits", totalcredits);//总积分
		jsonObject.put("members", page.getTotalCount());
		jsonObject.put("rewardList", list);
		jsonObject.put("currentPage", page.getPageNumber());
		jsonObject.put("totals", page.getPages());
		jsonObject.put("pageSize", page.getPageSize());
		jsonObject.put("totalcount", page.getTotalCount());
		return executeRerutn(jsonObject);
	}
	/**
	 * 排行榜
	 * @param currentPage 当前页
	 * @param pageSize 页大小
	 * @param userId 用户代码
	 * @return
	 */
	public JSONObject ranking(@Security
			String currentPage,
			@Security
			String pageSize,
			@Security
			String userId) {
		Page page = new Page();
		JSONObject jsonObject = new JSONObject();
		if(StringUtils.isBlank(userId)){
			throw new BusinessException(Constants.ERRORS.VALIDATE_ERROR_CODE,
					"用户代码不能为空");
		}
		if(StringUtils.isNotBlank(pageSize)){
			page.setPageSize(Integer.parseInt(pageSize));
		}
		if(StringUtils.isNotBlank(currentPage)){
			page.setPageNumber(Integer.parseInt(currentPage));
		}
		List<Member> list = this.list(this.getStatementName("rankingList"),new HashMap(), page);
		jsonObject.put("list", list);
		return jsonObject;
	}

}
